package com.happyma.bbn;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {

    private static final String TAG = "SettingsActivity-->";
    private BroadcastReceiver returnsToMainActivityReceiver;

    private void setBroadcastReceiverReturnToMainActivity() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("org.bbnradio.return.to.main.activity");
        returnsToMainActivityReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SettingsActivity.this.finish();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
                SettingsActivity.this.onBackPressed();
            }
        };
        registerReceiver(returnsToMainActivityReceiver, intentFilter);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
        }
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();
        setBroadcastReceiverReturnToMainActivity();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(returnsToMainActivityReceiver);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}